package com.jushu.storbox.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jushu.storbox.LoginActivity;
import com.jushu.storbox.LoginWaitActivity;
import com.jushu.storbox.R;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.util.PublicTools;

@SuppressLint("ValidFragment")
public class WelcomePagec extends Fragment implements OnClickListener {
    private RelativeLayout pageLayout;
    private Button startButton;
    private LayoutInflater layoutInflater;
    private View image;
    private Intent intent = null;
    private boolean isWelcomeButton;

    public WelcomePagec(boolean isWelcomeButton) {
        // TODO Auto-generated constructor stub
        this.isWelcomeButton = isWelcomeButton;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.layoutInflater = inflater;
        initialize();
        return pageLayout;
    }

    private void initialize() {
        pageLayout = (RelativeLayout) layoutInflater.inflate(R.layout.welcome_page_c_layout, null);
        image = pageLayout.findViewById(R.id.welcome_page_image_c);
        startButton = (Button) pageLayout.findViewById(R.id.welcome_page_button_c);
        startButton.setOnClickListener(this);
        initData();
    }

    private void initData() {
        if (PublicTools.isLocaleLanguageCN()) {
            image.setBackgroundResource(R.mipmap.welcome_page_bg_c);
            // startButton.setBackgroundResource(R.drawable.welcome_page_button_c_bg);
        } else {
            image.setBackgroundResource(R.mipmap.welcome_page_bg_c_en);
            // startButton.setBackgroundResource(R.drawable.welcome_page_button_c_en_bg);
        }
    }

    private void startProgram() {
        if (intent != null) {
            intent = null;
        }
        int versionCode = PublicTools.getVersionCode(getActivity());
        SharedPreferences settings = getActivity().getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
        Editor editor = settings.edit();
        editor.putInt("VERSIONCODE", versionCode);
        editor.commit();
        Intent intent = new Intent();
        if (isWelcomeButton) {
            intent.setClass(getActivity(), LoginWaitActivity.class);
        } else if (settings.getBoolean(ClientConfig.AUTOLOGIN, false)) {
            intent.setClass(getActivity(), LoginWaitActivity.class);
        } else {
            intent.setClass(getActivity(), LoginActivity.class);
        }
        getActivity().startActivity(intent);
        getActivity().finish();
        getActivity().overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(startButton)) {
            startProgram();
        }
    }
}
